<?php
/**
 * 记录日志
 * 
* @copyright	http://www.easymvc.com
* @author 		光哥  <439884988@qq.com>
* @version		1.0 - 2012-03-26
* @package		Frame
*/
class Frame_Log {
	
	const EXCEPTION = 1;
	const DEBUG = 2;
	const INFO  =3;
	
	
	public $log_dir;
	
	public function __construct(){
		$this->log_dir = Frame_Config::get('env.log_dir');
	}
	/**
	 * 实例化类
	 */
	public static function getInstance(){
		static $obj;
		if(!$obj) $obj = new self();
		return $obj;
	}
	/**
	 *  批量写入日志
	 *  @param array $data
	 *  @param string $log_file
	 */
	public function writeLog($data,$isMulti=0,$log_file='msg.log',$msg_type=3,$delimiter=' | '){
		$date = date('Y-m-d H:i:s ');
		$log = '';
		if($isMulti){
			foreach($datas as $data){
				$log .= $date.' | '.implode(' | ',$data)."\r\n";
			}
		}
		else{
			if(is_array($data)) $log = implode(' | ',$data);
			$log = $date.' | '.$log."\r\n";
		}
		$folder = self::selectFolder($msg_type);
		$destination = $this->log_dir.DS.$folder.DS.date('Y-m-d').'_'.$log_file;
		error_log($log,3,$destination);
	}
	/**
	 *  批量写入系统日志
	 *  @param array $datas
	 *  @param string $log_file
	 */
	public function sysLog($data,$isMulti=0){
		$date = date('Y-m-d H:i:s ');
		$log = '';
		if($isMulti){
			foreach($datas as $data){
				$log .= $date.' | '.implode(' | ',$data);
			}
		}else {
			if(is_array($data)) $log = implode(' | ',$data);
			$log = $date.' | '.$log;
		}
		
		openlog('mylog', LOG_PID | LOG_ODELAY,LOG_LOCAL4);
		syslog(LOG_INFO, $log);	
		closelog();	
	}
	
	/**
	 * 存在日志文件夹
	 * @param integer $msg_type
	 * @return string
	 */
	public static function selectFolder($msg_type){
		if($msg_type == self::INFO){
			$folder = 'info';
		}elseif($msg_type == self::DEBUG){
			$folder = 'debug';
		}else{
			$folder = 'exception';
		}
		return $folder;
	}
	
}

?>